package com.ycxy.school.express.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.ycxy.school.express.pojo.dto.OrderAddNewDTO;
import com.ycxy.school.express.pojo.dto.OrderUpdateDTO;
import com.ycxy.school.express.pojo.vo.OrderDetailVO;
import com.ycxy.school.express.pojo.vo.OrderListItemVO;
import com.ycxy.school.express.service.IOrderService;
import com.ycxy.school.express.web.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author jayLuo
 * @Date 2023/1/8 19:06
 * @PackageName:com.ycxy.school.express.controller
 * @ClassName: OrderController
 * @Description: TODO
 * @Version 1.0
 */
@Slf4j
@Api(tags = "05. 订单管理模块")
@RestController
@RequestMapping("/orders")
public class OrderController {

    @Autowired
    private IOrderService orderService;


    // http://localhost:9080/orders/create
    @ApiOperation("创建订单")
    @ApiOperationSupport(order = 100)
    @PostMapping("/create")
    public JsonResult<Void> createOrder(OrderAddNewDTO orderAddNewDTO) {
        log.debug("开始处理【创建订单】的请求，参数：{}", orderAddNewDTO);
        orderService.createOrder(orderAddNewDTO);
        return JsonResult.ok();
    }

    // http://localhost:9080/orders/9527/delete-user
    @ApiOperation("用户根据id手动删除订单")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id", value = "订单id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/delete-user")
    public JsonResult<Void> deleteByUser(@Range(min = 1, message = "删除订单失败，尝试删除的订单ID无效！")
                                   @PathVariable Long id) {
        log.debug("开始处理【根据id手动删除订单】的请求，参数：{}", id);
        orderService.deleteByUser(id);
        return JsonResult.ok();
    }

    // http://localhost:9080/orders/9527/delete-system
    @ApiOperation("系统根据id删除订单")
    @ApiOperationSupport(order = 210)
    @ApiImplicitParam(name = "id", value = "订单id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/delete-system")
    public JsonResult<Void> deleteBySystem(@Range(min = 1, message = "系统删除订单失败，尝试删除的订单ID无效！")
                                   @PathVariable Long id) {
        log.debug("开始处理【系统根据id删除订单】的请求，参数：{}", id);
        orderService.deleteBySystem(id);
        return JsonResult.ok();
    }

    // http://localhost:9080/orders/9527/delete-withdraw
    @ApiOperation("用户根据id撤销订单")
    @ApiOperationSupport(order = 220)
    @ApiImplicitParam(name = "id", value = "订单id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/delete-withdraw")
    public JsonResult<Void> withdrawOrder(@Range(min = 1, message = "用户撤销订单失败，尝试撤销的订单ID无效！")
                                           @PathVariable Long id) {
        log.debug("开始处理【用户根据id撤销订单】的请求，参数：{}", id);
        orderService.withdrawOrder(id);
        return JsonResult.ok();
    }

    // http://localhost:9080/orders/9527/restore
    @ApiOperation("还原订单")
    @ApiOperationSupport(order = 230)
    @ApiImplicitParam(name = "id", value = "订单id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/restore")
    public JsonResult<Void> restoreOrder(@Range(min = 1, message = "还原订单失败，尝试还原的订单ID无效！")
                                          @PathVariable Long id) {
        log.debug("开始处理【根据id还原订单】的请求，参数：{}", id);
        orderService.restoreOrder(id);
        return JsonResult.ok();
    }

    // http://localhost:9080/orders/9527/receive
    @ApiOperation("骑手接单")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParam(name = "id", value = "订单id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/receive")
    public JsonResult<Void> receiveOrder(@Range(min = 1, message = "骑手接单失败，尝试接的订单ID无效！")
                                          @PathVariable Long id,String sn) {
        log.debug("开始处理【骑手接单】的请求，参数：{}", id);
        orderService.receiveOrder(id,sn);
        return JsonResult.ok();
    }

    // http://localhost:9080/orders/9527/complete
    @ApiOperation("修改订单状态为完成")
    @ApiOperationSupport(order = 310)
    @ApiImplicitParam(name = "id", value = "订单id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/complete")
    public JsonResult<Void> setComplete(@Range(min = 1, message = "修改订单状态为完成失败，尝试修改的订单ID无效！")
                                         @PathVariable Long id) {
        log.debug("开始处理【修改订单状态为完成】的请求，参数：{}", id);
        orderService.setComplete(id);
        return JsonResult.ok();
    }

    // http://localhost:9080/orders/9527/error
    @ApiOperation("修改订单状态为异常")
    @ApiOperationSupport(order = 320)
    @ApiImplicitParam(name = "id", value = "订单id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/error")
    public JsonResult<Void> setError(@Range(min = 1, message = "修改订单状态为异常失败，尝试修改的订单ID无效！")
                                        @PathVariable Long id) {
        log.debug("开始处理【修改订单状态为完成】的请求，参数：{}", id);
        orderService.setError(id);
        return JsonResult.ok();
    }

    // http://localhost:9080/orders/9527/update
    @ApiOperation("修改订单信息")
    @ApiOperationSupport(order = 330)
    @ApiImplicitParam(name = "id", value = "订单id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/update")
    public JsonResult<Void> update(@Range(min = 1, message = "修改订单状态为完成失败，尝试修改的订单ID无效！")
                                        @PathVariable Long id, OrderUpdateDTO orderUpdateDTO) {
        log.debug("开始处理【修改订单信息】的请求，参数：{},{}", id,orderUpdateDTO);
        orderService.updateOrder(id,orderUpdateDTO);
        return JsonResult.ok();
    }

    // http://localhost:9080/orders
    @ApiOperation("查询订单列表")
    @ApiOperationSupport(order = 400)
    @GetMapping("")
    public JsonResult<List<OrderListItemVO>> list() {
        log.debug("开始处理【查询订单列表】的请求，无参数");
        List<OrderListItemVO> list = orderService.list();
        return JsonResult.ok(list);
    }

    // http://localhost:9080/orders/user
    @ApiOperation("根据用户id查询订单列表")
    @ApiOperationSupport(order = 410)
    @GetMapping("/user")
    public JsonResult<List<OrderListItemVO>> listByUserId() {
        log.debug("开始处理【根据用户id查询订单列表】的请求");
        List<OrderListItemVO> list = orderService.listByUserId();
        return JsonResult.ok(list);
    }

    // http://localhost:9080/orders/staff
    @ApiOperation("根据配送员id查询订单列表")
    @ApiOperationSupport(order = 420)
    @GetMapping("/staff")
    public JsonResult<List<OrderListItemVO>> listByStaffId() {
        log.debug("开始处理【根据配送员id查询订单列表】的请求");
        List<OrderListItemVO> list = orderService.listByStaffId();
        return JsonResult.ok(list);
    }

    // http://localhost:9080/orders/wait
    @ApiOperation("查询等待接单的订单列表")
    @ApiOperationSupport(order = 430)
    @GetMapping("/wait")
    public JsonResult<List<OrderListItemVO>> waitOrderList() {
        log.debug("开始处理【查询等待接单的订单列表】的请求，无参数");
        List<OrderListItemVO> list = orderService.waitOrderList();
        return JsonResult.ok(list);
    }

    // http://localhost:9080/orders/open
    @ApiOperation("查询评论已开启的订单列表")
    @ApiOperationSupport(order = 440)
    @GetMapping("/open")
    public JsonResult<List<OrderListItemVO>> listByEvaluate() {
        log.debug("开始处理【查询评论已开启的订单列表】的请求，无参数");
        List<OrderListItemVO> list = orderService.listByEvaluate();
        return JsonResult.ok(list);
    }

    // http://localhost:9080/orders/delete
    @ApiOperation("查询已删除的订单列表")
    @ApiOperationSupport(order = 450)
    @GetMapping("/delete")
    public JsonResult<List<OrderListItemVO>> listByDelete() {
        log.debug("开始处理【查询已删除的订单列表】的请求，无参数");
        List<OrderListItemVO> list = orderService.listByDelete();
        return JsonResult.ok(list);
    }

    // http://localhost:9080/orders/9527/detail
    @ApiOperation("根据id查询订单详情")
    @ApiOperationSupport(order = 460)
    @GetMapping("/{id:[0-9]+}/detail")
    public JsonResult<OrderDetailVO> getOrderDetail(
            @Range(min = 1, message = "查询订单详情失败，尝试查询的订单ID无效！")
            @PathVariable Long id) {
        log.debug("开始处理【查询订单详情】的请求，参数: {}",id);
        OrderDetailVO orderDetail= orderService.getOrderDetail(id);
        return JsonResult.ok(orderDetail);
    }

    // http://localhost:9080/orders/wait
    @ApiOperation("查询最新创建的订单")
    @ApiOperationSupport(order = 470)
    @GetMapping("/new")
    public JsonResult<OrderListItemVO> getOrderNew() {
        log.debug("开始处理【查询最新创建订单】的请求，无参数");
        OrderListItemVO newOrder = orderService.getOrderNew();
        return JsonResult.ok(newOrder);
    }

    // http://localhost:9080/orders/pay-time
    @ApiOperation("查询用户未支付的订单列表")
    @ApiOperationSupport(order = 480)
    @GetMapping("/pay-time")
    public JsonResult<List<OrderListItemVO>> listByPayTime() {
        log.debug("开始处理【查询用户未支付的订单列表】的请求，无参数");
        List<OrderListItemVO> list = orderService.listByPayTime();
        return JsonResult.ok(list);
    }

    // http://localhost:9080/orders/transport
    @ApiOperation("查询配送中的订单列表")
    @ApiOperationSupport(order = 490)
    @GetMapping("/transport")
    public JsonResult<List<OrderListItemVO>> transportOrderList() {
        log.debug("开始处理【查询配送中的订单列表】的请求，无参数");
        List<OrderListItemVO> list = orderService.transportOrderList();
        return JsonResult.ok(list);
    }

    // http://localhost:9080/orders/transport-staff
    @ApiOperation("根据配送员id查询配送中的订单列表")
    @ApiOperationSupport(order = 491)
    @GetMapping("/transport-staff")
    public JsonResult<List<OrderListItemVO>> transportOrderListByStaffId() {
        log.debug("开始处理【根据配送员id查询配送中的订单列表】的请求，无参数");
        List<OrderListItemVO> list = orderService.transportOrderListByStaffId();
        return JsonResult.ok(list);
    }

    // http://localhost:9080/orders/complete-staff
    @ApiOperation("根据配送员id查询订单完成的订单列表")
    @ApiOperationSupport(order = 492)
    @GetMapping("/complete-staff")
    public JsonResult<List<OrderListItemVO>> completeOrderListByStaffId() {
        log.debug("开始处理【根据配送员id查询订单完成的订单列表】的请求，无参数");
        List<OrderListItemVO> list = orderService.completeOrderListByStaffId();
        return JsonResult.ok(list);
    }

}
